var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/filterRules/component/filterRules/index.tsx
var filterRules_exports = {};
__export(filterRules_exports, {
  FilterRules: () => FilterRules
});
module.exports = __toCommonJS(filterRules_exports);
var import_functionalComponent = require("../../../../functionalComponent");
var import_util = require("../../../../util");
var import_types = require("../../types");
var import_rulesController = require("../rulesController");
var isDisabled = (props) => {
  return props.disabled === true;
};
var FilterRules = (props) => {
  const { component, disabled = false, value, onChange } = props;
  const {
    maxLevel = 3,
    maxSize = 100,
    notEmpty = { data: true, message: "必须有一条数据" },
    initValues
  } = !isDisabled(props) && props;
  const findRelationNode = (parentData, targetKey, needCurrent) => {
    var _a;
    const parentDataTemp = parentData;
    if (parentDataTemp.key === targetKey)
      return parentDataTemp;
    if (!((_a = parentDataTemp.children) == null ? void 0 : _a.length))
      return null;
    for (let i = 0; i < parentDataTemp.children.length; i++) {
      const current = parentDataTemp.children[i];
      if (current.key === targetKey)
        return needCurrent ? current : parentDataTemp;
      const node = findRelationNode(current, targetKey, needCurrent);
      if (node)
        return node;
    }
  };
  const handleAddCondition = (keyObj) => {
    const cloneData = import_util.iup.deepCopyEx(value);
    const appendNode = findRelationNode(cloneData, keyObj.key, keyObj.isOut);
    addCondition(appendNode, keyObj, initValues);
    onChange == null ? void 0 : onChange(cloneData);
  };
  const addCondition = (treeNode, keyObj, initRowValue) => {
    const key = keyObj.key;
    if (keyObj.isOut)
      return treeNode.children.push(
        Object.assign({}, { rowValues: initRowValue }, { key: import_util.iup.uniqueId(), level: treeNode.level })
      );
    const children = treeNode == null ? void 0 : treeNode.children;
    if (!children) {
      const newNode = {
        key: treeNode.key,
        level: treeNode.level + 1,
        type: import_types.ROW_PERMISSION_RELATION.AND,
        children: [
          { rowValues: treeNode.rowValues, key: import_util.iup.uniqueId(), level: (treeNode == null ? void 0 : treeNode.level) + 1 },
          { rowValues: initRowValue, key: import_util.iup.uniqueId(), level: (treeNode == null ? void 0 : treeNode.level) + 1 }
        ]
      };
      delete treeNode.rowValues;
      Object.assign(treeNode, newNode);
      return;
    }
    for (let i = 0; i < children.length; i += 1) {
      if (children[i].key !== key)
        continue;
      if ((treeNode == null ? void 0 : treeNode.level) <= maxLevel) {
        children[i] = {
          key: children[i].key,
          type: import_types.ROW_PERMISSION_RELATION.AND,
          level: (treeNode == null ? void 0 : treeNode.level) + 1,
          children: [
            Object.assign({}, children[i], {
              key: import_util.iup.uniqueId(),
              level: (treeNode == null ? void 0 : treeNode.level) + 1
            }),
            Object.assign({
              key: import_util.iup.uniqueId(),
              rowValues: initRowValue,
              level: (treeNode == null ? void 0 : treeNode.level) + 1
            })
          ]
        };
      }
    }
  };
  const handleDeleteCondition = (key) => {
    const cloneData = import_util.iup.deepCopyEx(value);
    const deleteNode = findRelationNode(cloneData, key, false);
    if (notEmpty.data && !(deleteNode == null ? void 0 : deleteNode.children))
      return import_functionalComponent.message.info(notEmpty.message);
    if (!notEmpty.data && !(deleteNode == null ? void 0 : deleteNode.children)) {
      return onChange == null ? void 0 : onChange(void 0);
    }
    deleteCondition(deleteNode, key);
    onChange == null ? void 0 : onChange(cloneData);
  };
  const deleteCondition = (parentData, key) => {
    var _a, _b;
    let parentDataTemp = parentData;
    parentDataTemp.children = (_a = parentDataTemp == null ? void 0 : parentDataTemp.children) == null ? void 0 : _a.filter((item) => item.key !== key);
    if (((_b = parentDataTemp == null ? void 0 : parentDataTemp.children) == null ? void 0 : _b.length) === 1) {
      const newChild = updateLevel(parentDataTemp.children[0]);
      const key2 = parentDataTemp.key;
      delete parentDataTemp.children;
      delete parentDataTemp.type;
      parentDataTemp = Object.assign(parentDataTemp, {
        ...newChild,
        key: key2,
        level: newChild.level
      });
    }
  };
  const updateLevel = (node) => {
    let newChildren;
    if (node.children)
      newChildren = node.children.map((element) => updateLevel(element));
    const newNode = {
      ...node,
      children: newChildren,
      level: (node == null ? void 0 : node.level) - 1
    };
    return newNode;
  };
  const handleChangeCondition = (key, type) => {
    const cloneData = import_util.iup.deepCopyEx(value);
    const changeNode = findRelationNode(cloneData, key, true);
    changeNode.type = type === import_types.ROW_PERMISSION_RELATION.AND ? import_types.ROW_PERMISSION_RELATION.OR : import_types.ROW_PERMISSION_RELATION.AND;
    onChange == null ? void 0 : onChange(cloneData);
  };
  const handleChangeRowValues = (key, values) => {
    const cloneData = import_util.iup.deepCopyEx(value);
    const changeNode = findRelationNode(cloneData, key, true);
    changeNode.rowValues = {
      ...changeNode.rowValues ?? {},
      ...values
    };
    onChange == null ? void 0 : onChange(cloneData);
  };
  return /* @__PURE__ */ React.createElement(
    import_rulesController.RulesController,
    {
      maxLevel,
      maxSize,
      disabled,
      value,
      component,
      onAddCondition: handleAddCondition,
      onDeleteCondition: handleDeleteCondition,
      onChangeCondition: handleChangeCondition,
      onChangeRowValues: handleChangeRowValues
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FilterRules
});
